package com.xmb.wcapi.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmb.wcapi.entity.SysMenu;
import com.xmb.wcapi.entity.SysRolePermission;
import com.xmb.wcapi.entity.SysUser;
import com.xmb.wcapi.service.ISysMenuService;
import com.xmb.wcapi.service.ISysRolePermissionService;

@Controller
public class IndexController {
	
	@Autowired
	private ISysMenuService iSysMenuService;
	
	@Autowired
	private ISysRolePermissionService iSysRolePermissionService;
	
	@RequestMapping("/index")
	public String index(Model model) {
		//获取当前用户
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		//根据当前用户角色查询拥有权限
		List<SysRolePermission> permissionList = iSysRolePermissionService.selectInfoList(new QueryWrapper<SysRolePermission>().eq("role_id", user.getRoleId()));
		Set<String> permissionIds = new HashSet<String>();
		for (SysRolePermission sysRolePermission : permissionList) {
			permissionIds.add(sysRolePermission.getPermissionId());
		}
		QueryWrapper<SysMenu> ew = new QueryWrapper<SysMenu>();
//		ew.isNull("supper_id").or().eq("supper_id", "");
		ew.and(wrapper -> wrapper.isNull("supper_id").or().eq("supper_id", ""));
		ew.in("id", permissionIds);
		if(permissionIds.size()==0) {
			ew.select("1!=1");
		}
		model.addAttribute("list", iSysMenuService.list(ew.orderByAsc("order_by_index")));
		user.setUserPassword(null);
		model.addAttribute("user", user);
		return "index";
	}
}
